package com.caxxb.dao.impl;

import com.caxxb.dao.GameDao;
import com.caxxb.entity.Game;
import com.caxxb.entity.User;
import com.caxxb.util.DBUtil;
import com.caxxb.util.PageData;

import java.time.LocalDate;
import java.util.List;

public class GameDaoImpl implements GameDao {

    //根据id查询游戏
    public List<Game> getGameById(int id){
        String sql="SELECT * from game where game_id = ?";
        List list = DBUtil.exQuery(sql, Game.class, id);
        return list;
    }

    //获取所有游戏分页
    @Override
    public PageData<Game> getGamePages(int typeid,String keywords,int pageNo,int pageSize) {
        String sql="select * from game where game_name like ? and typeid = ? and status =1";
        PageData pageData = DBUtil.exQueryByPage(sql, Game.class ,pageNo,pageSize,"%"+keywords+"%",typeid);
        return pageData;
    }

    //获取所有游戏
    @Override
    public PageData<Game> getGame(String keywords,int pageNo,int pageSize) {
        String sql="select game.*,game_type.typename from game inner join game_type on game.typeid = game_type.typeid where game_name like ? and status =1";
        PageData pageData = DBUtil.exQueryByPage(sql, Game.class ,pageNo,pageSize,"%"+keywords+"%");
        return pageData;
    }

    //获取所有游戏的方法
    @Override
    public List<Game> getGames() {
        String sql="SELECT * from game where status = 1";
        List list = DBUtil.exQuery(sql, Game.class);
        return list;
    }

    //增加游戏销量的方法
    @Override
    public boolean addSales(int id) {
        String sql="update game set sales=sales+1 where game_id =?";
        int result = DBUtil.exUpdate(sql, id);
        return result>0;
    }

    @Override
    public boolean delSales(int id) {
        String sql="update game set sales=sales-1 where game_id =?";
        int result = DBUtil.exUpdate(sql, id);
        return result>0;
    }

    //增加游戏的方法
    @Override
    public boolean addGame(String game_name, Double price, String game_developer, LocalDate release_time, String game_img, int typeid) {
        String sql="insert into  game values(null,?,?,?,?,?,?,1,0)";
        int result = DBUtil.exUpdate(sql,game_name,price,game_developer,release_time,game_img,typeid);
        return result>0;
    }

    //得到热销游戏图片
    @Override
    public List getFireImg() {
        String sql="SELECT * from game order BY sales desc limit 4";
        List list = DBUtil.exQuery(sql, Game.class);
        return list;
    }

    //得到最新游戏图片
    @Override
    public List getNewlImg() {
        String sql="SELECT * from game order BY release_time desc limit 3";
        List list = DBUtil.exQuery(sql, Game.class);
        return list;
    }


    @Override
    public boolean updateGame(int game_id, Game game) {
        String sql ="UPDATE `game` SET `game_name`=?, `price`=?, `game_developer`=?, `release_time`=?, `game_img`=?, `typeid`=? WHERE (`game_id`=?)";
        int result = DBUtil.exUpdate(sql,game.getGame_name(),game.getPrice(),game.getGame_developer(),game.getRelease_time(),game.getGame_img(),game.getTypeid(),game_id);
        return result>0;
    }

    @Override
    public boolean delGame(int game_id){
        String sql = "update game SET status = 0 WHERE game_id = ?";
        int result = DBUtil.exUpdate(sql,game_id);
        return result > 1;
    }


}
